//
//  XFCacheViewManager.m
//  Test
//
//  Created by ukongm on 17/5/18.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFCacheViewManager.h"

@interface XFCacheViewManager()

/** 复用池 */
@property(nonatomic,strong)NSMutableDictionary* cache;

@end

@implementation XFCacheViewManager

- (NSMutableDictionary *)cache{
    
    if (_cache == nil) {
        _cache = [NSMutableDictionary new];
    }
    return _cache;
}

#pragma mark - 处理方法(私有)

//抛出异常
- (void)_exception:(NSString*)value{
    [NSException raise:@"XFCacheViewManager" format:@"%@",value];
}

//获取对应索引上的控件(没创建则创建)
- (UIView*)_getCacheViewForIndex:(NSInteger)index{
    NSString* key = [NSString stringWithFormat:@"%ld",index];
    
    UIView* view = [self.cache objectForKey:key];
    
    if (view == nil) {
        if (self.craeteComplate) {
            view = self.craeteComplate(index);
            
            [self.cache setObject:view forKey:key];
        }else{
            [self _exception:@"craeteComplate，没有赋值"];
        }
    }
    
    return view;
}

#pragma mark - 自定义方法
- (NSArray<UIView *> *)getCacheViewsForCount:(NSInteger)count{
    
    [self.cache.allValues makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSMutableArray* temp = [NSMutableArray new];
    
    for (NSInteger i = 0;  i < count; i++) {
        
        UIView* view = [self _getCacheViewForIndex:i];
        
        if (self.settingComplate) {
            self.settingComplate(view,i);
        }else{
            [self _exception:@"settingComplate，没有赋值"];
        }
    }
    
    return temp;
}



@end




















